package com.andatsoft.laisim.dialog;

import java.util.ArrayList;
import java.util.List;

import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.andatsoft.laisim.R;
import com.andatsoft.laisim.activity.BaseActivity;
import com.andatsoft.laisim.adapter.CreatePlaylistDialogSongListAdapter;
import com.andatsoft.laisim.db.DataBaseAccess;
import com.andatsoft.laisim.entity.Playlist;
import com.andatsoft.laisim.entity.Song;
import com.andatsoft.laisim.utils.Common;
import com.andatsoft.laisim.utils.ToastMessage;

public class CreatePlaylistDialog extends BaseActivity {

	private ListView mListView;
	private EditText mEtPlaylistName;
	private TextView mTvOk;
	private List<Integer> mListSelected;
	private List<Song> mSongList;
	private CreatePlaylistDialogSongListAdapter mAdapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getWindow().requestFeature(Window.FEATURE_NO_TITLE);
		getWindow().setBackgroundDrawable(
				new ColorDrawable(getSelectedTheme()
						.getDialogBackgroundColor()));
		setContentView(R.layout.dialog_create_playlist);
		mListSelected = new ArrayList<Integer>();
		mSongList = new ArrayList<Song>();
		initViews();
		setupListeners();
		fillData();
	}

	private void initViews() {
		mListView = (ListView) findViewById(R.id.list_dialog_create_playlist);
		mEtPlaylistName = (EditText) findViewById(R.id.et_dialog_create_playlist_name);
		mEtPlaylistName.setBackgroundColor(getSelectedTheme()
				.getDialogTextColor());
		mEtPlaylistName.setTextColor(getSelectedTheme()
				.getDialogBackgroundColor() | 0xff000000);
		mTvOk = (TextView) findViewById(R.id.tv_create_playlist_dialog_ok);
		mTvOk.setTextColor(getSelectedTheme().getDialogTextColor());
		((TextView) findViewById(R.id.tv_title_dialog_create_playlist))
				.setTextColor(getSelectedTheme().getDialogTextColor());
//		((TextView) findViewById(R.id.tv_static_dialog_create_playlist_name))
//				.setTextColor(getSelectedTheme().getDialogTextColor());
	}

	private void setupListeners() {
		mListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> adapter, View view, int pos,
					long arg3) {
				if (mListSelected.contains(pos)) {
					mListSelected.remove((Integer) pos);
				} else {
					mListSelected.add(pos);
				}
				mAdapter.setSongSelected(mListSelected);
			}
		});

		mTvOk.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				switch (event.getAction()) {
				case MotionEvent.ACTION_DOWN:
					setAlpha(v, 0.5f);
					return true;

				case MotionEvent.ACTION_UP:
					setAlpha(v, 1.0f);
					List<Song> result = new ArrayList<Song>();
					for (Integer pos : mListSelected) {
						result.add(mSongList.get(pos));
					}
					if (result.size() > 0) {
						DataBaseAccess db = DataBaseAccess.getInstance(getApplicationContext());
						String playlistName = mEtPlaylistName.getText()
								.toString();
						if (playlistName == null || playlistName.equals("")) {
							playlistName = getString(R.string.common_text_playlist_default);
						}
						Playlist pl = new Playlist(playlistName, result);
						if (db.addPlaylist(pl)) {
							new ToastMessage(getApplicationContext())
									.showLongMessage(getString(R.string.create_playlist_add_ok));
						} else {
							new ToastMessage(getApplicationContext())
									.showLongMessage(getString(R.string.create_playlist_add_failed));
						}
					}
					setResult(Common.RESULT_CODE_CREATE_PLAYLIST);
					finish();
					return true;
				}

				return false;
			}
		});
	}

	private void fillData() {
		DataBaseAccess db = DataBaseAccess.getInstance(getApplicationContext());
		mSongList = db.getAllSong();
		mAdapter = new CreatePlaylistDialogSongListAdapter(
				getApplicationContext(), mSongList, mListSelected);
		mListView.setAdapter(mAdapter);
	}
}
